package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbBrandExample;
import com.pinyougou.sellergoods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper brandMapper;

    public PageInfo<TbBrand> findAll(Integer page, Integer size, TbBrand brand) {
        //分页
        PageHelper.startPage(page, size);
        //条件查询
        TbBrandExample example = new TbBrandExample();
        TbBrandExample.Criteria criteria1 = example.createCriteria();
        TbBrandExample.Criteria criteria2 = example.createCriteria();

        if (brand != null) {
            //首字母搜索
            if (brand.getFirstChar() != null && brand.getFirstChar().length() > 0 && brand.getFirstChar().trim().length() > 0) {
                criteria2.andFirstCharLike(brand.getFirstChar());
                System.out.println(brand.getFirstChar());
            }
            //名字模糊搜索
            if (brand.getName() != null && brand.getName().length() > 0 && brand.getName().trim().length() > 0) {
                criteria1.andNameLike(brand.getName());
                System.out.println(brand.getName());
            }

        }
        //执行查询
        List<TbBrand> all = brandMapper.selectByExample(example);

        return new PageInfo<TbBrand>(all);
    }

    public int add(TbBrand brand) {
        return brandMapper.insertSelective(brand);
    }

    /*
    根据Id查询
     */
    public TbBrand getById(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改品牌
     */
    public int updateBrand(TbBrand brand) {
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 执行删除操作
     */
    public int delete(List<Long> ids) {
        int i = 0;
        for (Long id : ids) {
            i = brandMapper.deleteByPrimaryKey(id);
        }
        return i;
    }
}
